﻿using CarlifeService;
using Commonality.DTO.AuthorityDTO;
using ICarlifeRespository;
using ICarlifeService;
using Models;
using Models.Authority;

namespace CarlifeService
{
    public class RolesService : BaseServer<Roles>, IRolesService
    {
        private readonly IRolesRepository db;
        private readonly IUOfWork uOfWork;
        public RolesService(IRolesRepository db, IUOfWork uOfWork) : base(db, uOfWork)
        {
            this.db = db;
            this.uOfWork = uOfWork;
        }

        public int GetRoleAdd(Roles entity)
        {
            db.Add(entity);
            return uOfWork.SaveChanges();
        }

        public List<RolesDTO> GetRoleAll()
        {
            var list = from x in db.GetAll()
                       select new RolesDTO
                       {
                           RoleID = x.Id,
                           Parent_id = x.Parent_id,
                           Account = x.Account,
                           RoleName = x.RoleName,
                           Description = x.Description,
                           IsDelet = x.IsData,
                       };
            list = list.Where(x => x.IsDelet == true);
            return list.ToList();
        }

        public Roles GetRoleByid(int id)
        {
            return db.GetById(id);
        }

        public int GetRoleRemove(int id)
        {
            var list=db.GetById(id);
            list.IsData = false;
            db.Update(list);
            return uOfWork.SaveChanges();
        }

        public int GetRoleUpdate(Roles entity)
        {
            db.Update(entity);
            return uOfWork.SaveChanges();
        }
    }
}
